package com.example.service;


import cn.hutool.core.date.DateTime;
import com.example.common.enums.ResultCodeEnum;
import com.example.entity.RegistrationForm;
import com.example.exception.CustomException;
import com.example.mapper.RegistrationFormMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 挂号单业务处理
 **/
@Service
public class RegistrationFormService {

    @Resource
    private RegistrationFormMapper registrationFormMapper;

    /**
     * 新增
     */
    public void add(RegistrationForm registrationForm) {
        RegistrationForm dbRegistrationForm = registrationFormMapper.selectByFormNumber(registrationForm.getRegistrationNumber());
        if (dbRegistrationForm != null) {
            throw new CustomException(ResultCodeEnum.REGISTRATION_FORM_NUMBER_REPEAT);
        }
        //随机生成一个挂号单的订单表号
        registrationForm.setRegistrationNumber(String.valueOf(System.currentTimeMillis()));
        registrationFormMapper.insert(registrationForm);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        registrationFormMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            registrationFormMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(RegistrationForm registrationForm) {
        RegistrationForm dbRegistrationForm = registrationFormMapper.selectByFormNumber(registrationForm.getRegistrationNumber());
        if (dbRegistrationForm != null && !dbRegistrationForm.getId().equals(registrationForm.getId())) {
            throw new CustomException(ResultCodeEnum.REGISTRATION_FORM_NUMBER_REPEAT);
        }
        registrationForm.setUpdatedTime(String.valueOf(DateTime.now()));
        registrationFormMapper.updateById(registrationForm);
    }

    /**
     * 根据ID查询
     */
    public RegistrationForm selectById(Integer id) {
        return registrationFormMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<RegistrationForm> selectAll(RegistrationForm registrationForm) {
        return registrationFormMapper.selectAll(registrationForm);
    }

    /**
     * 分页查询
     */
    public PageInfo<RegistrationForm> selectPage(RegistrationForm registrationForm, Integer pageNum, Integer pageSize) {
        System.out.println(registrationForm);
        PageHelper.startPage(pageNum, pageSize);
        List<RegistrationForm> list = registrationFormMapper.selectAll(registrationForm);
        return PageInfo.of(list);
    }

    /**
     * 根据医生的id和查询条件进行分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<RegistrationForm> selectPageInDoctor(RegistrationForm registrationForm,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RegistrationForm> list = registrationFormMapper.selectPageInDoctor(registrationForm);
        System.out.println(list);
        return PageInfo.of(list);
    }
}